<?php
session_start();
// 增强安全验证
if (!isset($_SESSION['logged_in']) || !$_SESSION['logged_in']) {
    header('HTTP/1.1 403 Forbidden');
    exit('Access denied');
}
include 'config.php';
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>سۇپا-<?php echo $website_name; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <header class="admin-header">
        <h3>مكىرو مۇنبەر سىسىتىمىسى</h3>
        <nav>
            <a href="index.php" class="button">باشبەت首页</a>
            <a href="yezix.php" class="button">قۇرۇش新建</a>
            <a href="logout.php" class="button">قايتىش退出</a>
        </nav>
    </header>
    
    <main class="admin-main">
        <div class="management-tools">
            <form method="get" class="search-form">
                <input type="text" name="search" placeholder="بى كۆرەي..." value="<?php echo isset($_GET['search']) ? htmlspecialchars($_GET['search']) : ''; ?>">
                <button type="submit" class="button">ئىزدەش</button>
            </form>
        </div>
        <div class="article-grid" style="display: flex; flex-wrap: wrap; gap: 20px;">
            <?php
            $articlesDir = 'articles/';
            $articles = [];
            $searchKeyword = isset($_GET['search']) ? trim($_GET['search']) : '';

            if ($handle = opendir($articlesDir)) {
                while (($file = readdir($handle)) !== false) {
                    if ($file !== "." && $file !== ".." && pathinfo($file, PATHINFO_EXTENSION) === 'html') {
                        $articles[] = $file;
                    }
                }
                closedir($handle);
                
                // 按修改时间倒序排序
                usort($articles, function($a, $b) use ($articlesDir) {
                    return filemtime($articlesDir.$b) - filemtime($articlesDir.$a);
                });

                foreach ($articles as $file) {
                    $articleFile = $articlesDir . $file;
                    $content = file_get_contents($articleFile);
                    
                    // 提取元数据
                    preg_match('/<h1>(.*?)<\/h1>/is', $content, $titleMatches);
                    preg_match('/<meta name="description" content="(.*?)"\s*\/?>/i', $content, $descMatches);
                    
                    $title = $titleMatches[1] ?? '未命名文章';
                    $description = $descMatches[1] ?? '暂无描述';
                    $editTime = date("Y-m-d H:i", filemtime($articleFile));

                    // 搜索逻辑
                    if (empty($searchKeyword) || 
                        stripos($title, $searchKeyword) !== false || 
                        stripos($description, $searchKeyword) !== false) {
                        echo <<<HTML
                        <div class="management-item" style="flex: 1 1 calc(33.33% - 13px); 
                            background-color: #fff;
                            padding: 6px;
                            border: 1px solid #ddd;
                            border-radius: 12px;
                            box-sizing: border-box;
                            transition: box-shadow 0.3s, transform 0.3s;
                            display: flex;
                            flex-direction: column;
                            justify-content: space-between;">
                            <div class="item-header">
                                <h2>{$title}</h2>
                                <span class="file-info">{$file}</span>
                            </div>
                            <p class="item-desc">{$description}</p>
                            <div class="item-meta">
                                <time style="font-size: 0.8em; color: #666; display: block; margin-top: 5px;">最后修改：{$editTime}</time>
                                <div class="item-actions">
                                    <a href="article.php?file={$file}" target="_blank" class="button">كۆرەش</a>
                                    <a href="edit.php?file={$file}" class="button">يېزىش</a>
                                    <a href="delete.php?file={$file}" 
                                       class="button danger" 
                                       onclick="return confirm('确定永久删除该文章？')">يۇيۇش</a>
                                </div>
                            </div>
                        </div>
HTML;
                    }
                }
            }
            ?>
        </div>
    </main>
</body>
</html>
